package com.example.a24porint;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;

public class localgame extends AppCompatActivity implements View.OnClickListener{

    int[] num=new int[4];
    int[][] color=new int[3][];
    private Button[] bt=new Button[4];
    private Button[] wy=new Button[5];
    private Button chexiao=null;
    private Button huan1ti=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_localgame);
        init();
    }
    public void turncolor(View v) {
        switch (v.getId())
        {
            case R.id.button:
                if(color[0][0]==0) {
                    ((Button)v).setBackgroundColor(Color.parseColor("#4AFA00"));
                    color[0][0] = 1;
                }
                else {
                    ((Button)v).setBackgroundColor(Color.parseColor("#A0FF78"));
                    color[0][0] = 0;
                }
                break;
            case R.id.button2:
                if(color[0][2]==0) {
                    ((Button)v).setBackgroundColor(Color.parseColor("#4AFA00"));
                    color[0][2] = 1;
                }
                else {
                    ((Button)v).setBackgroundColor(Color.parseColor("#A0FF78"));
                    color[0][2] = 0;
                }
            break;
            case R.id.button3:
                if(color[2][0]==0) {
                    ((Button)v).setBackgroundColor(Color.parseColor("#4AFA00"));
                    color[2][0] = 1;
                }
                else {
                    ((Button)v).setBackgroundColor(Color.parseColor("#A0FF78"));
                    color[2][0] = 0;
                }
                break;
            case R.id.button5:
                if(color[2][2]==0) {
                    ((Button)v).setBackgroundColor(Color.parseColor("#4AFA00"));
                    color[2][2] = 1;
                }
                else {
                    ((Button)v).setBackgroundColor(Color.parseColor("#A0FF78"));
                    color[2][2] = 0;
                }
                break;
            case R.id.button8:
                if(color[1][0]==0) {
                    ((Button)v).setBackgroundColor(Color.parseColor("#4AFA00"));
                    color[1][0] = 1;
                }
                else {
                    ((Button)v).setBackgroundColor(Color.parseColor("#A0FF78"));
                    color[1][0] = 0;
                }
                break;
            case R.id.button9:
                if(color[1][2]==0) {
                    ((Button)v).setBackgroundColor(Color.parseColor("#4AFA00"));
                    color[1][2] = 1;
                }
                else {
                    ((Button)v).setBackgroundColor(Color.parseColor("#A0FF78"));
                    color[1][2] = 0;
                }
                break;
            case R.id.button6:
                if(color[0][1]==0) {
                    ((Button)v).setBackgroundColor(Color.parseColor("#4AFA00"));
                    color[0][1] = 1;
                }
                else {
                    ((Button)v).setBackgroundColor(Color.parseColor("#A0FF78"));
                    color[0][1] = 0;
                }
                break;
            case R.id.button7:
                if(color[2][1]==0) {
                    ((Button)v).setBackgroundColor(Color.parseColor("#4AFA00"));
                    color[2][1] = 1;
                }
                else {
                    ((Button)v).setBackgroundColor(Color.parseColor("#A0FF78"));
                    color[2][1] = 0;
                }
                break;
            default:
                break;
        }
    }
    @Override
    public void onClick(View v) {
        //#4AFA00
               // ((Button)v).setBackgroundColor();
        turncolor(v);
        switch (v.getId())
        {
            case R.id.button10:
                resetall();break;
            case R.id.button11:
                huan1tirun();break;
            case R.id.button:
            case R.id.button2:
            case R.id.button3:
            case R.id.button5:
                onClickButton(v);
                break;
            case R.id.button8:way=1;break;
            case R.id.button9:way=2;break;
            case R.id.button6:way=3;break;
            case R.id.button7:way=4;break;
            default:
                break;
        }
    }

    private void huan1tirun() {
        Random a=new Random();
        for(int i=0;i<4;i++)
        {
            num[i]=a.nextInt(14);
            resetall();
        }
    }

    private void resetall() {
        c1=0;diyishu=0;remain=4;way=0;strdiyishu="";buttontemp=null;
        for(int i=0;i<4;i++)
        {
            bt[i].setText(""+num[i]);
            bt[i].setVisibility(View.VISIBLE);
        }
    }

    private int c1=0,diyishu=0,remain=4,way=0;
    private String strdiyishu="";
    private Button buttontemp;
    private void onClickButton(View v)
    {
        String str=((Button)v).getText().toString();
        if(c1==0)
        {
            c1=1;
            /*if(existchu(str))
            {
                diyishu=-5201314;
                strdiyishu=str;
            }
            else
            diyishu=Integer.valueOf(str);*/
            buttontemp=(Button)v;
        }
        else if(c1==1&&way==0)
        {
            turncolor(v);
            turncolor(buttontemp);
            /*if(existchu(str))
            {
                diyishu=-5201314;
                strdiyishu=str;
            }
            else
            diyishu=Integer.valueOf(str);*/
            buttontemp=(Button)v;
        }
        else if(c1==1&&way!=0)
        {
            String strresult=calculation(buttontemp,(Button)v);
            if(strresult!="") {
                buttontemp.setText(strresult);
                turncolor(buttontemp);
                turncolor(v);
                turncolor(wy[way]);
                way = 0;
                c1 = 0;
                remain--;
                if (remain == 1) {
                    //wy[1].setText("24你信吗？");
                    int theresult = 0;
                    for (int i = 0; i < 4; i++) {
                        if (bt[i].getVisibility() != View.INVISIBLE) {
                            //if(existchu(bt[i].toString()))continue;
                            theresult = Integer.valueOf(bt[i].getText().toString());
                        }
                    }
                    if(theresult==24)
                    {
                        Toast ts = Toast.makeText(getBaseContext(),"你赢了",Toast.LENGTH_LONG);
                        ts.show();
                    }
                    else
                    {
                        Toast ts = Toast.makeText(getBaseContext(),"你输了",Toast.LENGTH_LONG);
                        ts.show();
                    }
                }
            }
        }
    }
    private boolean existchu(String str)
    {
        for(int i=0;i<str.length();i++)
        {
            if(str.charAt(i)=='/')return true;
        }
        return false;
    }
    private String chucal(String str1,String str2)
    {
        int str1qian=0,str1hou=0,str2qian=0,str2hou=0;
        String strresult="";
        if(existchu(str1))
        {
            int chusitu=-1;
            for(int i=0;i<str1.length();i++)
            {
                if(str1.charAt(i)!='/') {
                    str1qian += (int)str1.charAt(i) - (int)'0';
                    str1qian*=10;
                }
                else {
                    chusitu=i;
                    str1qian/=10;
                    break;
                }
            }
            if(chusitu!=-1)
            {
                for(int i=chusitu+1;i<str1.length();i++)
                {
                    str1hou+=(int)str1.charAt(i)-(int)'0';
                    str1hou*=10;
                }
                str1hou/=10;
            }
        }
        else{
            for(int i=0;i<str1.length();i++)
            {
                str1qian += (int)str1.charAt(i) - (int)'0';
                str1qian*=10;
            }
            str1hou=1;
            str1qian/=10;
        }
        if(existchu(str2))
        {
            int chusitu=-1;
            for(int i=0;i<str2.length();i++)
            {
                if(str2.charAt(i)!='/') {
                    str2qian += (int)str2.charAt(i) - (int)'0';
                    str2qian*=10;
                }
                else {
                    chusitu=i;
                    str2qian/=10;
                    break;
                }
            }
            if(chusitu!=-1)
            {
                for(int i=chusitu+1;i<str2.length();i++)
                {
                    str2hou+=(int)str2.charAt(i)-(int)'0';
                    str2hou*=10;
                }
                str2hou/=10;
            }

        }
        else{
            for(int i=0;i<str2.length();i++)
            {
                str2qian += (int)str2.charAt(i) - (int)'0';
                str2qian*=10;
            }
            str2qian/=10;
            str2hou=1;
        }
        if(str1hou!=str2hou)
        {
            str1qian*=str2hou;
            str2qian*=str1hou;
            str1hou*=str2hou;
            str2hou=str1hou;
        }
        //System.out.println(str1qian+"/"+str1hou);
        //System.out.println(str2qian+"/"+str2hou);
        switch (way) {
            case 1:
                str1qian += str2qian;
                strresult += new String("" + str1qian + "/" + str1hou);
                strresult = yuefen(strresult);
                break;
            case 2:
                str1qian -= str2qian;
                strresult += new String("" + str1qian + "/" + str1hou);
                strresult = yuefen(strresult);
                break;
            case 3:
                str1qian *= str2qian;
                str1hou *= str2hou;
                strresult += new String("" + str1qian + "/" + str1hou);
                strresult = yuefen(strresult);
                break;
            case 4:
                str1qian *= str2hou;
                str1hou *= str2qian;
                strresult += new String("" + str1qian + "/" + str1hou);
                strresult = yuefen(strresult);
                break;
            default:
                break;
        }
        return strresult;
    }
    private String yuefen(String str1)
    {
        int str1qian=0,str1hou=0,chusitu=0;
        String result="";
        for(int i=0;i<str1.length();i++)
        {
            if(str1.charAt(i)!='/') {
                str1qian += (int)str1.charAt(i) - (int)'0';
                str1qian*=10;
            }
            else {
                chusitu=i;
                str1qian/=10;
                break;
            }
        }
        for(int i=chusitu+1;i<str1.length();i++)
        {
            str1hou+=(int)str1.charAt(i)-(int)'0';
            str1hou*=10;
        }
        str1hou/=10;
        if(str1qian%str1hou==0)
        {
            str1qian/=str1hou;
            result=new String(""+str1qian);
        }
        else
        {
            int a=gcd(str1qian,str1hou);
            str1qian/=a;
            str1hou/=a;
            result=new String(""+str1qian+"/"+str1hou);
        }
        return result;
    }
    public int gcd(int m, int n) {
        int result = 0;
        while (n != 0) {result = m % n;
            m = n;
            n = result;
        }
        return m;
    }
    private String calculation(Button b1,Button b2)
    {
        if(b1.equals(b2))
        {
            //turncolor(b1);
            return "";
        }
        if(way==0)return "";
        int result=0;
        int a1=0,a2=0;
        String str1=b1.getText().toString(),str2=b2.getText().toString(),strresult="";
        if(existchu(str1)||existchu(str2))
        {
            strresult=chucal(str1,str2);
            b2.setVisibility(View.INVISIBLE);
            return strresult;
        }
        else {
            a1 = Integer.valueOf(str1);
            a2 = Integer.valueOf(str2);
            switch (way) {
                case 1:
                    a1 += a2;
                    break;
                case 2:
                    a1 -= a2;
                    break;
                case 3:
                    a1 *= a2;
                    break;
                case 4:
                    if (a1 % a2 == 0)
                        a1 /= a2;
                    else {
                        b2.setVisibility(View.INVISIBLE);
                        return a1 + "/" + a2;
                    }
                    break;
                default:
                    break;
            }
        }
        b2.setVisibility(View.INVISIBLE);
        return new String("" + a1);
    }
    public void init()
    {
        for(int i=0;i<3;i++)
        {
            color[i]=new int[3];
        }
        bt[0]=findViewById(R.id.button);
        //bt[0].setBackgroundColor(Color.parseColor("#4AFA00"));
        //bt[0].setBackgroundColor(Color.parseColor("#A0FF78"));
        bt[1]=findViewById(R.id.button2);
        bt[2]=findViewById(R.id.button3);
        bt[3]=findViewById(R.id.button5);
        wy[1]=findViewById(R.id.button8);
        wy[2]=findViewById(R.id.button9);
        wy[3]=findViewById(R.id.button6);
        wy[4]=findViewById(R.id.button7);
        chexiao=findViewById(R.id.button10);
        huan1ti=findViewById(R.id.button11);
        bt[0].setOnClickListener(this);
        bt[1].setOnClickListener(this);
        bt[2].setOnClickListener(this);
        bt[3].setOnClickListener(this);
        wy[1].setOnClickListener(this);
        wy[2].setOnClickListener(this);
        wy[3].setOnClickListener(this);
        wy[4].setOnClickListener(this);
        chexiao.setOnClickListener(this);
        huan1ti.setOnClickListener(this);
        Random r = new Random(1);
        for(int i=0;i<4;i++)
        {
            num[i]=r.nextInt(14);
            bt[i].setText(new String(String.valueOf(num[i])));
        }
    }


}